package com.kgcdemo.service;

import com.kgcdemo.entity.KGCParam;
import com.kgcdemo.sm9.gm.sm9.*;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

@Service
public class KGCService {
    SM9Curve sm9Curve = new SM9Curve();
    KGC kgc = new KGCWithStandardTestKey(sm9Curve);

    //注册标识，并返回相应的签名私钥
    public String getSignUserPrivateKey(String id, String ip) throws Exception {

        KGCWithStandardTestKey.k = new BigInteger(KGCParam.ks, 16);
        MasterKeyPair signMasterKeyPair = kgc.genSignMasterKeyPair();
        PrivateKey signPrivateKey = kgc.genPrivateKey(signMasterKeyPair.getPrivateKey(), id, PrivateKeyType.KEY_SIGN);
        return signPrivateKey.toPureString();
    }
    public Map<String, String> exchange(String id) throws Exception {

        KGCWithStandardTestKey.k = new BigInteger(KGCParam.ke, 16);
        MasterKeyPair encryptMasterKeyPair = kgc.genEncryptMasterKeyPair();
        PrivateKey exchangePrivateKey = kgc.genPrivateKey(encryptMasterKeyPair.getPrivateKey(), id, PrivateKeyType.KEY_KEY_EXCHANGE);
        Map<String, String> map = SM9Utils.toHexString(SM9Utils.getByte(exchangePrivateKey.d));
        return map;
    }

    //生成用户加密私钥
    public Map<String, String> getEncryptUserPrivateKey(String id, String ip) throws Exception {

        KGCWithStandardTestKey.k = new BigInteger(KGCParam.ke, 16);
        MasterKeyPair encryptMasterKeyPair = kgc.genEncryptMasterKeyPair();
        PrivateKey encryptPrivateKey = kgc.genPrivateKey(encryptMasterKeyPair.getPrivateKey(), id, PrivateKeyType.KEY_ENCRYPT);
//        System.out.println(encryptPrivateKey.d.getX().toBigInteger().toString(16));
//        System.out.println(encryptPrivateKey.d.getX().toBigInteger().toString(10));
//        System.out.println(encryptPrivateKey.d.getY().toBigInteger().toString(16));
//        System.out.println(encryptPrivateKey.d.getY().toBigInteger().toString(10));
//
//        System.out.println(SM9Utils.toHexString(encryptPrivateKey.d.getX().toBytes()));
//        System.out.println(SM9Utils.toHexString(encryptPrivateKey.d.getY().toBytes()));
//        System.out.println(SM9Utils.toHexString(SM9Utils.G2ElementToByte(encryptPrivateKey.d)));
//        return encryptPrivateKey.toPureString();
        Map<String, String> map = SM9Utils.toHexString(SM9Utils.getByte(encryptPrivateKey.d));
        return map;
    }

    //获取签名主公钥
    public Map<String, String> getSignMasterPublicKey(String id, String ip) {
        Map<String, String> Ppub_s = new HashMap<>();
        Ppub_s.put("x_x","9F64080B3084F733E48AFF4B41B565011CE0711C5E392CFB0AB1B6791B94C408");
        Ppub_s.put("x_y","29DBA116152D1F786CE843ED24A3B573414D2177386A92DD8F14D65696EA5E32");
        Ppub_s.put("y_x","69850938ABEA0112B57329F447E3A0CBAD3E2FDB1A77F335E89E1408D0EF1C25");
        Ppub_s.put("y_y","41E00A53DDA532DA1A7CE027B7A46F741006E85F5CDFF0730E75C05FB4E3216D");
        return Ppub_s;
    }

    //获取加密主公钥
    public String getEncryptMasterPublicKey(String id, String ip) {
        return KGCParam.Ppub_e;
    }
}
